/*
Wolke Lin 
blog:http://wolkesau.pixnet.net/blog
email:wolkesau@gmail.com
=========================
1.do the script code creater~(add wolke personal data at the div)
2.Wolke Red Chinese to Taiwaness language translater

across web server just can use get way to save data ,
but it`s not usful ,
ask user link to GAE server,(or iframe in the wolke blog)
then JSON data(save into GAE format) can save in the GAE
get all the data, 
user can select what he want
but whaterver user save in the GAE
it would give to all user
note in the user tag
use jqgrid

system repJSON data grid
commond repJSON data grid
ex: red repJSON data grid
your grid can include red data and your data

when type the new 

2009.1119

		1.This JQuery plugin can help auto update words which you type in the text box.
		
		2.The idea is from a female famous(maybe) taiwaness author who like to say something bad about a women who take her former boyfriend,she like  to add (old) before the woman , but she always say it is the system made it.
		then I create it , ha~
		Wolke Lin 
		email:wolkesau@gmail.com
		blog:http://wolkesau.pixnet.net/blog/
		====example===================
		1.ori:orignal word
		2.rep:replace word
		3.prefixes: prefixes word
		4.suffixes: suffixes word
		
		var repJson=[{'ori':'123','rep':'456','prefixes':'','suffixes':''},
				{'ori':'abc','rep':'abc','prefixes':'d','suffixes':'e'}];		
		
		$('.content').autoUpdateWords(repJson);
		
		
		2009.1118*/
$.fn.WL_autoUpdateWords=function(repJSON)
	{		
		function setString(str,splitChar)
		{/*2009.1118*/
			var list=[];
			while(true)
			{		
				try
				{
				list.push(str.split(splitChar,splitChar.length)[0]);}	
				catch(er){return false}
				
				if (str.search(splitChar)>=0)
				{str=str.substr(str.search(splitChar)+splitChar.length)}
				else
				{
					if (list==[])
					{return false}
					break;
				}				
			}
			return list;
		}
		function updateString(list,rep,pre,suf)
		{	
			if(list.length<2)
			{return false}
			var new_str='';
			for(var i=0;i<list.length-1;i++)
			{
				var index=list[i].length-pre.length;
				try{
					if(new String(list[i]).substr(index)!=pre)
					{new_str+=list[i]+pre + rep}
					else
					{new_str+=list[i]+rep}
					if(new String(list[i+1]).substr(0,suf.length)!=suf)
					{new_str+=suf}			
				}catch(er){break;}		
			}
			new_str+=list[list.length-1];
			return new_str;
		}
		return this.each(function(){

			$(this).keyup(function()
			{
				for(var i=0;i<repJSON.length;i=i+1)
				{
					str=$(this).val();		
					var ans='';			
					var list=setString($(this).val(),repJSON[i]['ori']);
					ans=updateString(list,repJSON[i]['rep'],repJSON[i]['prefixes'],repJSON[i]['suffixes']);
					if(ans==false)
					{continue;}
					else
					{str=ans;
					$(this).val(str);}
				}
			});
		})		
	}
